package com.zf.shuashua.advert;

import android.text.TextUtils;
import android.util.Log;

import com.meis.base.mei.utils.ParseJsonUtils;
import com.zf.shuashua.api.AbsCallback;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.advert.MistakeBean;
import com.zf.shuashua.bean.advert.MistakeConfigBean;
import com.zf.shuashua.bean.advert.MistakeTimeBean;
import com.zf.shuashua.utils.TimeUtils;
import com.zf.shuashua.utils.Util;

import java.util.Calendar;
import java.util.Date;

/**
 * 误触管理类
 */
public class MistakeConfigManager {

    private static final int TYPE_OPEN = 1;
    private static final int TYPE_DRAW = 2;
    private static final int TYPE_NATIVE = 3;
    private MistakeConfigBean mistakeConfig;

    private static MistakeConfigManager instance;

    private MistakeConfigManager() {
    }

    public static MistakeConfigManager getInstance() {
        if (null == instance) {
            synchronized (MistakeConfigManager.class) {
                if (null == instance) {
                    instance = new MistakeConfigManager();
                }
            }
        }
        return instance;
    }

    /**
     * 同步上次的配置
     */
    public void syncConfig(){
        try{
            String strConfig = Store.getInstance().getMistakeConfig();
            if(!TextUtils.isEmpty(strConfig)){
                mistakeConfig = ParseJsonUtils.parseData(strConfig, MistakeConfigBean.class);
            }
        }catch (Exception ignored){

        }
    }

    /**
     * 拉取最新的误触配置
     */
    public void fetchConfig(String city) {
        HttpData.getInstance().getPatchMistakeSettings(city, new AbsCallback<BaseData<MistakeConfigBean>>() {
            @Override
            public void onRealSuccess(BaseData<MistakeConfigBean> data) {
                if (data == null || data.getData() == null) {
                    return;
                }
                if(data.getCode() != 200){
                    return;
                }
                mistakeConfig = data.getData();
                String json = ParseJsonUtils.toJson(mistakeConfig);
                Store.getInstance().setMistakeConfig(json);
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                Log.e("fetchConfig",  errCode + "");
            }
        });
    }

    /**
     * 开屏广告是否误触
     * @return
     */
    public boolean isDisSkip() {
        return isDisCommon(TYPE_OPEN);
    }

    /**
     * draw信息流是否误触
     * @return
     */
    public boolean isDisDraw() {
        return isDisCommon(TYPE_DRAW);
    }

    /**
     * 贴片广告是否误触
     * @return
     */
    public boolean isDisNative() {
        return isDisCommon(TYPE_NATIVE);
    }

    private boolean isDisCommon(int type){
        Store store = Store.getInstance();
        //第一次启动不误触
        if (store.isADNewUser()) {
            return false;
        }
        //误触关闭
        if (mistakeConfig == null || mistakeConfig.getEnable() == 0) {
            return false;
        }
        //误触时间段配置
        MistakeTimeBean[] mistakeTimes = mistakeConfig.getMistake_open_times();
        if(mistakeTimes == null || mistakeTimes.length <= 0){
            return false;
        }
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        boolean isContain = false;
        for(MistakeTimeBean mistakeTime : mistakeTimes){
            if(mistakeTime != null ){
                if(hour >= mistakeTime.getStart() && hour <= mistakeTime.getEnd()){
                    isContain = true;
                    break;
                }
            }
        }
        if(!isContain){
            return false;
        }
        MistakeBean config = mistakeConfig.getAdvert_patch_mistake_open();
        if(type == TYPE_OPEN){
            config = mistakeConfig.getAdvert_start_ad_mistake_open();
        }else if(type == TYPE_DRAW){
            config = mistakeConfig.getAdvert_info_mistake_open();
        }else if(type == TYPE_NATIVE){
            config = mistakeConfig.getAdvert_patch_mistake_open();
        }
        if(config == null){
            return false;
        }
        if(config.getStatus() == 0){
            return false;
        }
        //误触概率
        int min = config.getStar_rate();
        int max = config.getEnd_rate();
        int num = Util.getRandom(min, max);
        return num == min;
    }

}
